package com.djdg.foodies.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @Description:
 * @Auther Demon
 * @Date 2018/1/28 0028 12:58 星期日
 */
public class DateTimeUtil {

    public DateTimeUtil() {
    }

    public static String dateTimeToString(Date date) {
        SimpleDateFormat sdfdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdfdt.format(date);
    }

    public static Date stringToDateTime(String dateString) throws ParseException {
        SimpleDateFormat sdfdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdfdt.parse(dateString);
    }

    public static Date stringToDate(String dateString) throws ParseException {
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return sdfd.parse(dateString);
    }

    public static String dateToString(Date date) {
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return sdfd.format(date);
    }

    public static String chineseDateString(Date date) {
        SimpleDateFormat chineseDateSdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
        return chineseDateSdf.format(date);
    }

    public static Date getFirstDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getFirstDateOfLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        calendar.add(2, -1);
        return calendar.getTime();
    }

    public static Date getBeginTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return calendar.getTime();
    }

    public static Date getTheEndOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2, 11);
        calendar.set(5, calendar.getActualMaximum(5));
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return calendar.getTime();
    }

    public static Date addDays(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, days.intValue());
        return calendar.getTime();
    }

    public static Date addHours(Date date, Integer hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(11, hours.intValue());
        return calendar.getTime();
    }

    public static Date addMonths(Date date, Integer months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, months.intValue());
        return calendar.getTime();
    }

    public static boolean isFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(5) == 1;
    }

    public static int calcAgeByBirthday(Date birthDay) {
        if (birthDay == null || birthDay.after(new Date())) {
            return -1;
        } else {
            Calendar cal = Calendar.getInstance();
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        }
    }

    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(7) == 1 ? 7 : cal.get(7) - 1;
    }

    public static int getHour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(11);
    }

    public static boolean isTheSameDay(Date one, Date another) {
        return dateToString(one).equals(dateToString(another));
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(isTheSameDay(new Date(), new Date()));
    }

}
